	
	$(document).ready(function(){
		$(document).find('form').each(function(i){
			makeReadOnly($(this));
			$(this).submit(function(){
				return doSubmitForm($(this));
			})			
		});
		
		$("#tinhthanhsel").change(function(){			
			var tinhthanh = $(this).val();			
			$.ajax({
				type:"GET",
				url: "/getlist.html?type=quanhuyen&parent="+tinhthanh
			}).done(function(data) {					
					$("#divquanhuyen").html(data);
			});				
		});
		
		$(document).find("select.select").each(function(){	
			var view = $(this).hasClass("view");
			$(this).multiselect({
				header: false,
				noneSelectedText: $(this).attr("title"),
				selectedList: 1,
				multiple: false,
				minWidth: $(this).width()					
			});
			
			if(view){
				$(this).multiselect("disable")
			}
		});
		

		$(document).find("select.multiselect").each(function(){
			var view = $(this).hasClass("view");

			$(this).multiselect({
				header: "Chọn tối đa 10",
				noneSelectedText: $("select.multiselect").attr("title"),			
				selectedList: 10,
				click: function(e){
					if( $(this).multiselect("widget").find("input:checked").length > 10 ){
						return false;
					}else{
						
					}
				}
			});

			
			
			if(view){
				$(this).multiselect("disable")
			}
				
		});
		
		$("#frmRegister").find("input[name='username']:first").change(function(){			
			$("#frmRegister").find("input[name='email']:first").val($(this).val());
		});
		
		$("#imgrefresh").click(function(){
			var d = new Date();		
			$($(this).attr("href")).attr("src","/code.php?d"+d);
			return false;
		});
		
		$("a.nophoso").click(function(){
			$(this).removeClass("nophoso");
			var url = $(this).attr("href");
			$.ajax({
				url: url,
				type: "GET"
			}).done(function(html){
				$("#nopdon").html(html);
			});
			
			return false;
		});
	})

	function doSubmitForm(frmObj){	
		var error = "";
		var redpoint = '';// '<font color="#FF0000" class="redpoint">(*)</font>';

		frmObj.find('.input_form[rel]').each(function(i){
			$(this).parent().find('.redpoint').each(function(i){ $(this).remove(); });
			$(this).removeClass("errorborder");
			
			var rel = $(this).attr("rel");
			var alt = $(this).attr("alt");			
			var value = $(this).val();

			if(rel.indexOf("*") >= 0 && (trim(value) == "" || value == alt)){
				$(this).addClass("errorborder");
				$(this).parent().append(redpoint);
				error = "Vui lòng điền đầy đủ các thông tin bắt buộc";
			}
		});
		
		if(error == ""){
			error = otherSubmitFunction(frmObj);
		}
		
		if(error != ""){		
			var errorContain = frmObj.find('#error').first();
			if(errorContain.length > 0){
				errorContain.text(error);
				errorContain.show('fast');
				$('html, body').animate({scrollTop:0}, 'fast');
				return false;
			}else{
				alert(error);
				return false;
			}
		}else
			return true;
	}
	
	function otherSubmitFunction(frmObj){
		var error = "";
		if(frmObj.attr("name") == "frmMemberRegister"){
			var img = frmObj.find("#btn_image:first").attr("title");			
			if(img == "Hình Đại Diện"){
				var hdd = frmObj.find("input[name='simage']").first();				
				if(hdd.length == 0 || hdd.val() == ""){
					error = "Vui lòng upload Ảnh đại diện";
				}
			}
			
			var passObj = frmObj.find("input[name='password']").first();
			var repassObj = frmObj.find("input[name='repassword']").first();
			
			var password = $(passObj).val();
			var repassword = $(repassObj).val();
			if(password != repassword){
				error = "Mật khẩu không trùng khớp!";
				$(passObj).addClass("errorborder");
				$(repassObj).addClass("errorborder");
			}
			
			if(error == ""){				
				if(frmObj.find("input[name='accept']").first().is(':checked') == false){
					error = "Vui lòng chấp nhận quy định Thành Viên của website";
				}
			}
		}else if(frmObj.attr("name") == "frmMemberChangePassword"){
			var passObj = frmObj.find("input[name='newpassword1']").first();
			var repassObj = frmObj.find("input[name='newpassword2']").first();
			if(passObj.val() != repassObj.val()){
				error = "Mật khẩu không trùng khớp!";
				$(passObj).addClass("errorborder");
				$(repassObj).addClass("errorborder");
			}
		}
		
		return error;
	}
	
	$(document).ready(function(){
		$(document).find('form').each(function(i){
			var obj = $(this);
			var objForm = this;
			obj.find('#edit').click(function(){
				$(this).hide();
				obj.find("#view").show();
				obj.find("#update").show();
				changeClassName(obj, '.input_form', 'view', 'edit', true);		
				obj.find("select.select").multiselect("enable");
				obj.find("select.multiselect").multiselect("enable");
			});
			
			obj.find('#view').click(function(){
				$(this).hide();
				obj.find("#update").hide();
				obj.find("#edit").show();
				objForm.reset();
				changeClassName(obj, '.input_form', 'edit', 'view', true);	
				obj.find("select.select").multiselect("disable");
				obj.find("select.multiselect").multiselect("disable");				
			});
		});	

		$("#thanphien").click(function(){
			$('form#frmTinDang').attr({"target":"_self","action":"/tin-rao/than-phien.html"});
			$('form#frmTinDang').submit();	
		});
		
		$("#banin").click(function(){
			var $url = "/tin-rao/ban-in/" +  $(this).attr("rel")+".html";
			$('form#frmTinDang').attr({"action":$url});
			$('form#frmTinDang').submit();	
		});
	});
		
	function changeClassName(obj, findWhat, remove, add, makeReadOnly){
		obj.find(findWhat).each(function(i){			
		
			if(makeReadOnly)
				changeReadOnlyStatus($(this));
				
			$(this).removeClass(remove);
			$(this).addClass(add);
		});
			
	}
	
	function changeReadOnlyStatus(obj){			
		obj.attr("readonly",!obj.attr("readonly"));
	}
	
	function makeReadOnly(objForm){		
		objForm.find('.view').each(function(i){			
			$(this).attr('readonly',true);
		});
	}
	
	$(document).ready(function(){
		$(document).find('input[type="text"], textarea').each(function(i){
			if($(this).attr("title") != ""){
				makeInActive($(this));
				$(this).focus(function(){makeActive($(this));});
				$(this).blur(function(){makeInActive($(this));});
			}
		});
	});
	
	function makeActive(obj){
		var title = obj.attr("title");
		if(obj.val() == title){
			obj.val("");
		}
		
		obj.addClass("active");
		obj.removeClass("inactive");
	}
	
	function makeInActive(obj){
		var title = obj.attr("title");
		
		if(obj.val() == ""){
			obj.val(title);
			obj.addClass("inactive");
			obj.removeClass("active");
		}
		
		
	}
	
	
	
	
	
	